import 'dart:convert';

import 'package:baseproject/model/carousel/carousel.dart';
import 'package:baseproject/network/httpUtil.dart';
import 'package:baseproject/view/home/carouse.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomeState();
  }
}

class _HomeState extends State<Home> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  // 轮播图组件
  GlobalKey<CarouselWidgetState> carouselKey = GlobalKey();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getCarouselData();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
      width: MediaQuery.of(context).size.width,
      height: 250.0,
      child: CarouselWidget(carouselKey),
    ));
  }

  getCarouselData() async {
    CarouselList carouselList;
    await HttpUtil().post(
        "http://a.game.163.com/fz/interface/frontend/fz.do?pos=eveduanyou(MA79)-lunhuan-264163",
        success: (data) {
      var returnData = json.decode(data);
      carouselList = new CarouselList.fromJson(returnData["result"]["content"]);
    });
    if (carouselList != null) {
      carouselKey.currentState.setCarouselList(carouselList.data);
    }
  }
}
